# -*- coding: utf-8 -*-
import random
import ConfigSpace as CS
import ConfigSpace.hyperparameters as CSH
import itertools

from .Conv import Conv

class ConvInteger(Conv):
    def __init__(self):
        print('ConvInteger Init--------------------------')
        super().__init__()

    def input_shape_correct(self, shapes):
        print('input_shape_correct, ConvInteger')

        if len(shapes) < 2 or len(shapes) > 4:
            return False

        if len(shapes[0]) > 4 :
            shapes[0] = shapes[0][:4]

        if len(shapes[1]) > 4 :
            shapes[1] = shapes[1][:4]   

        if len(shapes[0]) < 3 and len(shapes[1]) < 3 :
            if len(shapes[0]) % 2 == 0:
                #print('reshape input 111111111')
                for i in range(4-len(shapes[0])):
                    shapes[0].append(random.randint(1, 8))
            else:
                #print('reshape input 222222222222222')
                for i in range(3-len(shapes[0])):
                    shapes[0].append(random.randint(8, 16))

        if len(shapes[0]) != len(shapes[1]) : 
            if len(shapes[0]) < 3:
                shapes[0] = shapes[1].copy()
            elif len(shapes[1]) < 3:
                shapes[1] = shapes[0].copy()    
            else:
                if len(shapes[0]) % 2 == 0:
                    #print('change shapes[1]', shapes[1], shapes[0])
                    shapes[1] = shapes[0].copy()
                else:
                    #print('change shapes[0]', shapes[1], shapes[0])
                    shapes[0] = shapes[1].copy()                       

        #print('-----before change shapes[1][0]', shapes[1][0])

        shapes[1][0] = random.randint(shapes[0][0], 10*shapes[0][0])

        #print('-----after change shapes[1][0]', shapes[1][0], shapes[0][0])

        v = int(shapes[0][1]/shapes[1][1])
        if v != 0:
            shapes[0][1] = v * shapes[1][1]
        else :
            v = int(shapes[1][1]/shapes[0][1])
            if v != 0 :
                shapes[0][1], shapes[1][1] = shapes[1][1], shapes[0][1]
                shapes[0][1] = v * shapes[1][1]
            else:    
                shapes[0][1] = shapes[1][1]

        v =  int(shapes[0][1]/shapes[1][1])
        tmp = int(shapes[1][0]/v)
        if tmp == 0:
            tmp = v

        shapes[1][0] = tmp * v

        print('set group: ', shapes[0][1], shapes[1][1], v, tmp, shapes[1][0])          

        #print('-----before change shapes[0][2]', shapes[0][2])

        shapes[0][2] = random.randint(shapes[1][2], 2*shapes[1][2])

        print('-----after change shapes[0][2]', shapes[0][2])

        #print('last length:', len(shapes[0]), len(shapes[1]))

        if shapes[1][2] > shapes[0][2] : 
            print('ERROR, should not go here~~~~', shapes[1][2], shapes[0][2])
            shapes[1][2] = shapes[0][2] 

        if len(shapes[0]) == 4:
            #print('-----before change shapes[0][3]', shapes[0][3])
            shapes[0][3] = random.randint(shapes[1][3], 2*shapes[1][3])
            #print('-----after change shapes[0][3]', shapes[0][3])

            if shapes[1][3] > shapes[0][3] : 
                print('XXXX ERROR, should not go here~~~~', shapes[1][2], shapes[0][2])
                shapes[1][3] = shapes[0][3]

        if len(shapes) >= 3:
            v = random.randint(0, 1)
            if v == 0:
                shapes[2] = []
            else:
                shapes[2] = [1] 

        if len(shapes) == 4:
            v = random.randint(0, 1)
            if v == 0:
                shapes[3] = []
            else:
                shapes[3] = [1]             

        #for dim in enumerate(shapes[0]):
        #    print('last shapes[0]: ', dim)    

        #for dim in enumerate(shapes[1]):
        #    print('last shapes[1]: ', dim)        

    def get_output_type(self, index):
        return 'tensor(int32)'    